﻿using System;
using System.IO;
using sinetd.Utilities;

namespace ftp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "ftp [sinetd]";

            Console.WriteLine();
            Console.WriteLine("File Transfer");

            sinetd.Config cfg = new sinetd.Config();

            cfg.SetDefault("protocol", "Tcp");
            cfg.SetDefault("port", "3000");

            cfg.SetDefault("host", "localhost");

            ConsoleAddons.ParseArguments(args, cfg, new string[] { "host" });

            Console.WriteLine(cfg.PrintDictionary());

            FileTransfer ft = new FileTransfer(cfg);
            if (ft.Connect())
            {
                string[] msg = new string[1] { string.Empty };

                while (msg[0] != "exit")
                {
                    //Console.Clear();
                    Console.Write("> ");
                    msg = Console.ReadLine().Split(' ');

                    switch (msg[0])
                    {
                        case "ls":
                            string[] list = ft.ls();
                            foreach (string l in list)
                            {
                                Console.WriteLine(l);
                            }
                            break;
                        case "put":
                            if (File.Exists(msg[1]))
                            {
                                ft.put(msg[1]);
                                Console.WriteLine("File sended!");
                            }
                            else
                            {
                                Console.WriteLine("Provide proper path");
                            }
                            break;
                        case "get":
                            ft.get(msg[1]);
                            Console.WriteLine("File saved!");
                            break;

                        case "exit":
                            ft.Disconnect();
                            Console.WriteLine("Good Bye!");
                            break;

                        case "help":
                            Console.WriteLine(" ls   - gets list of files on server");
                            Console.WriteLine(" put local_path  - puts files on server");
                            Console.WriteLine(" get remote_path - gets file from server");
                            Console.WriteLine(" help - this message");
                            Console.WriteLine(" exit - exists program");
                            break;
                        default:
                            Console.WriteLine("Try help");
                            break;
                    }
                }
            }
            else
            {
                Console.WriteLine("Host doesn't answer.");
            }
            //Console.ReadKey();
        }
    }
}
